
/**
 * AppleIIGo
 * Internal SmartPort 2 IWM class
 * (C) 2007-2008 by Marc S. Ressl (mressl@umich.edu)
 * Released under the GPL
 */

#include "InternalSmartPort2IWM.h"

char * internalSmartPort2IWMDriveList[] = {
	"Apple525DiskDrive",
	"Apple525DiskDrive",
	"Apple35DiskDrive",
	"Apple35DiskDrive",
	"UniDisk35DiskDrive",
	"UniDisk35DiskDrive",
	"SmartPortDiskDrive",
	"SmartPortDiskDrive",
	0,
};

InternalSmartPort2IWM::InternalSmartPort2IWM(Motherboard * motherboard,
											 char * slotDeviceName) :
IWM(motherboard, slotDeviceName) {
	connectDrives(internalSmartPort2IWMDriveList);
}

void InternalSmartPort2IWM::onDevicesConnected() {
	IWM::onDevicesConnected();
	mmu = motherboard->getMmu();
}

void InternalSmartPort2IWM::onResetAsserted(bool isRestart) {
	IWM::onResetAsserted(isRestart);
	setDiskRegister(0);
}

void InternalSmartPort2IWM::writeIo(UINT16 address, UINT8 value) {
	if ((address & 0xff) == 0x31) {
		setDiskRegister(value);
		return;
	}
	
	DiskIIController::writeIo(address, value);
}

UINT8 InternalSmartPort2IWM::readIo(UINT16 address) {
	if ((address & 0xff) == 0x31)
		return getDiskRegister();
	
	return DiskIIController::readIo(address);
}

UINT8 InternalSmartPort2IWM::readMemory(UINT16 address) {
	return mmu->readMemoryDirect(address);
}

void InternalSmartPort2IWM::setDiskRegister(UINT8 value) {
	diskRegister = value;
	
	isDrive35Enabled = getBit(value, DISKREGISTER_DRIVE35);
	updateSelectedPort();
	
	selectedPort->setControl(getBit(value, DISKREGISTER_HEAD1));
}

UINT8 InternalSmartPort2IWM::getDiskRegister() {
	return diskRegister;
}
